package cn.craccd.mongoHelper.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ZipUtil;

@Component
public class ModuleUtils {
	String projectPath;
	String startPath;

	public void build(String name, String chineseName, Boolean buildHtml, Boolean override) {
		try {
			initPath();

			List<String> paths = new ArrayList<String>();
			findFileList(startPath, paths);

			for (String path : paths) {
				if (path.endsWith(".html") || path.endsWith(".js")) {
					if (!buildHtml) {
						continue;
					}
				}

				List<String> contents = FileUtil.readLines(path, CharsetUtil.UTF_8);
				List<String> newContents = new ArrayList<String>();
				for (String content : contents) {
					content = content.replace("User", firstToUpcase(name));
					content = content.replace("user", name);
					content = content.replace("用户", chineseName);
					newContents.add(content);
				}

				String[] pathArray = path.split("module");
				String destFilePath = projectPath + pathArray[1].replace("user", name).replace("User", firstToUpcase(name));
				FileUtil.writeLines(newContents, destFilePath, CharsetUtil.UTF_8, override);
				System.out.println("write:" + destFilePath);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			FileUtil.del(startPath);
		}
	}

	public void remove(String name) {
		try {
			initPath();

			List<String> paths = new ArrayList<String>();
			findFileList(startPath, paths);

			for (String path : paths) {
				String[] pathArray = path.split("module");
				String destFilePath = projectPath + pathArray[1].replace("user", name).replace("User", firstToUpcase(name));
				if (destFilePath.endsWith("index.js")) {
					destFilePath = destFilePath.replace("index.js", "");
				}
				if (destFilePath.endsWith("index.html")) {
					destFilePath = destFilePath.replace("index.html", "");
				}
				FileUtil.del(destFilePath);
				System.out.println("delete:" + destFilePath);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			FileUtil.del(startPath);
		}
	}

	private void initPath() throws IOException {
		File libTemp = FileUtil.getTmpDir();

		ClassPathResource resource = new ClassPathResource("static/module.zip");
		InputStream inputStream = resource.getInputStream();
		FileUtil.writeFromStream(inputStream, libTemp + File.separator + "module.zip");
		FileUtil.mkdir(libTemp + File.separator + "module");
		ZipUtil.unzip(libTemp + File.separator + "module.zip", libTemp + File.separator + "module");
		FileUtil.del(libTemp + File.separator + "module.zip");
		startPath = libTemp + File.separator + "module";

		projectPath = ResourceUtils.getFile("classpath:").getPath().replace("target" + File.separator + "test-classes", "");

	}

	public void findFileList(String path, List<String> fileNames) {
		File dir = new File(path);
		if (!dir.exists() || !dir.isDirectory()) {// 判断是否存在目录
			return;
		}
		String[] files = dir.list();// 读取目录下的所有目录文件信息
		for (int i = 0; i < files.length; i++) {// 循环，添加文件名或回调自身
			File file = new File(dir, files[i]);
			if (file.isFile()) {// 如果文件
				fileNames.add(dir + "\\" + file.getName());// 添加文件全路径名
			} else {// 如果是目录
				findFileList(file.getPath(), fileNames);// 回调自身继续查询
			}
		}
	}

	public String firstToUpcase(String str) {
		return str.substring(0, 1).toUpperCase().concat(str.substring(1).toLowerCase());
	}
}
